<!--
 * @Descripttion:
 * @version:
 * @Author: YouHuJun
 * @Date: 2021-09-22 14:51:37
 * @LastEditors: youhujun 2900976495@qq.com
 * @LastEditTime: 2025-04-08 18:34:23
 * @LastEditTime: 2021-09-23 18:51:53
-->
<template>
  <div>
    <el-form
      ref="addGoodsSpecNameForm"
      :model="addGoodsSpecNameForm"
      :rules="addGoodsSpecNameRules"
      label-width="150px"
      :inline="true"
      label-position="left"
      status-icon
      inline-message
      style="width:80%;margin-left:auto;margin-right: auto;padding:30px 20px;"
    >
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="规格名名称" prop="spec_name">
            <el-input v-model="addGoodsSpecNameForm.spec_name" placeholder="规格名名称" class="input" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="是否是默认选">
            <el-select v-model="addGoodsSpecNameForm.is_default_option" placeholder="请选择">
              <el-option label="否" :value="0" />
              <el-option label="是" :value="1" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="规格名编码" prop="spec_code">
            <el-input v-model="addGoodsSpecNameForm.spec_code" placeholder="规格名编码" class="input" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="24">
          <el-form-item label="规格名备注" prop="note">
            <el-input v-model="addGoodsSpecNameForm.note" placeholder="规格名备注" class="input" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="16">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="addGoodsSpecNameForm.sort" class="input" />
          </el-form-item>
        </el-col>
        <el-col :span="8" style="vertical-align: middle;line-height:40px;">数值越大,顺序靠前,默认100</el-col>
      </el-row>
      <el-row type="flex" justify="center" style="margin-top:30px">
        <el-col :span="12" :offset="4">
          <el-form-item>
            <el-button type="primary" @click="submitForm('addGoodsSpecNameForm')">添加系统配置</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="4">
          <el-form-item>
            <el-button @click="resetForm('addGoodsSpecNameForm')">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { addGoodsSpecName } from '@/api/youhu-shop/V1/goods/goodsSpec'
import { debounce } from '@/utils'
export default
{
  // 组件名称
  name: 'AddSpecConfigForm',
  props:
    {

    },
  // 数据
  data()
  {
    return {
      // 添加菜单表单
      addGoodsSpecNameForm: {
        // 规格名名称
        spec_name: '',
        // 规格名编码
        spec_code: '',
        // 是否是默认选项 0否 1是
        is_default_option: 0,
        // 备注
        note: '',
        // 排序
        sort: 100
      },
      // 添加表单规则
      addGoodsSpecNameRules: {
        spec_name: [
          { required: true, message: '请输入规格名名称', trigger: 'blur' }
        ],
        sort: [
          { pattern: /^[0-9]{1,10}$/, message: '只能0-9的数字', trigger: 'blur' }
        ]
      }
    }
  },
  // 计算属性
  computed:
  {
  },
  // 监听
  watch:
  {
  },
  // 实例创建之前
  beforeCreate()
  {
  },
  // 创建
  created()
  {

  },
  // 挂载之前
  beforeMount()
  {

  },
  // 挂载 --常用
  mounted()
  {

  },
  // 更新之前
  beforeUpdate()
  {
  },
  // 跟新后
  updated()
  {
  },
  // 销毁之前
  beforeDestroy()
  {
  },
  // 销毁后
  destroyed()
  {
  },
  // 方法
  methods:
  {
    initSpecConfigForm()
    {
      this.$refs.addGoodsSpecNameForm.resetFields()
      this.addGoodsSpecNameForm = {}
    },
    // 提交表单
    submitForm: debounce(function(formName)
    {
      this.$refs[formName].validate((valid) =>
      {
        if (valid)
        {
          this.addGoodsSpecName(this.addGoodsSpecNameForm)
        }
        else
        {
          console.log('未通过验证!')
          return false
        }
      })
    }, 600),

    // 重置表单
    resetForm(formName)
    {
      this.initSpecConfigForm()
    },

    /**
     * 添加系统配置项
     */

    addGoodsSpecName(data)
    {
      if (data.voice_url && !data.voice_path)
      {
        data.voice_path = data.voice_url
      }
      const that = this
      addGoodsSpecName(data).then(res =>
      {
        if (res && res.code === 0)
        {
          that.$emit('handleCloseDialog', false)
          that.$message(
            {
              message: res.msg,
              type: 'success'
            })
          that.initSpecConfigForm()
        }
      })
    }

  }
}
</script>
<style lang='scss' scoped>
.select,
.input {
  width: 300px;
}
.upload-submit {
  margin-left: 10px;
  font-size: 12px;
  color: #409eff;
  &:hover {
    text-decoration:underline
  }
}
</style>
